﻿using System;
using System.Windows;
using System.Windows.Browser;

namespace Gmantis.Controls
{
    internal static class DomUtil
    {
        // Fields
        private static ScriptObject JS_GetPluginParam;
        private static ScriptObject JS_GetXY;
        private static ScriptObject JS_Measure;

        // Methods
        static DomUtil()
        {
            if (HtmlPage.IsEnabled)
            {
                JS_GetXY = (HtmlPage.Window.Eval("[\r\n            function(el) {\r\n                var x = 0, y = 0;\r\n                p = el;\r\n                while (p) {\r\n                    x += p.offsetLeft;\r\n                    y += p.offsetTop;\r\n                    p = p.offsetParent;\r\n                }\r\n                return [x,y];\r\n            }]") as ScriptObject).GetProperty("0") as ScriptObject;
                JS_Measure = (HtmlPage.Window.Eval("[\r\n                function(el, availableWidth) {\r\n                    var sizeEl = document.createElement('div')\r\n                    document.body.appendChild(sizeEl)\r\n                    sizeEl.style.position='absolute'\r\n                    sizeEl.style.zIndex=-20000\r\n                    sizeEl.style.left=0\r\n                    sizeEl.style.top=0\r\n                    sizeEl.style.width = availableWidth==Infinity? '100%' : availableWidth + 'px'\r\n                    sizeEl.style.height='100%'\r\n                    sizeEl.innerHTML = el.innerHTML\r\n                    var size = [Math.max(sizeEl.firstChild.clientWidth, sizeEl.firstChild.offsetWidth), Math.max(sizeEl.firstChild.clientHeight, sizeEl.firstChild.offsetHeight)]\r\n                    document.body.removeChild(sizeEl)\r\n                    return size\r\n                }]") as ScriptObject).GetProperty("0") as ScriptObject;
                JS_GetPluginParam = (HtmlPage.Window.Eval("[\r\n                function(plugin, name) {\r\n                    for(var i=0; i<plugin.childNodes.length; ++i)\r\n                        if(plugin.childNodes[i].tagName == 'PARAM' && plugin.childNodes[i].name.toLowerCase() == name)\r\n                            return plugin.childNodes[i].value\r\n                    return ''\r\n                }]") as ScriptObject).GetProperty("0") as ScriptObject;
            }
        }

        public static Point GetLocation(HtmlElement element)
        {
            ScriptObject obj2 = JS_GetXY.InvokeSelf(new object[] { element }) as ScriptObject;
            return new Point((double)obj2.GetProperty("0"), (double)obj2.GetProperty("1"));
        }

        public static string GetPluginParam(string name)
        {
            return (string)JS_GetPluginParam.InvokeSelf(new object[] { HtmlPage.Plugin, name.ToLower() });
        }

        public static Size GetSize(HtmlElement element)
        {
            double width = double.Parse(element.GetProperty("clientWidth").ToString());
            return new Size(width, double.Parse(element.GetProperty("clientHeight").ToString()));
        }

        public static Size Measure(HtmlElement el, double availableWidth)
        {
            ScriptObject obj2 = JS_Measure.InvokeSelf(new object[] { el, availableWidth }) as ScriptObject;
            return new Size((double)obj2.GetProperty("0"), (double)obj2.GetProperty("1"));
        }

        // Properties
        public static Point SilverlightLocation
        {
            get
            {
                return GetLocation(HtmlPage.Plugin);
            }
        }

        public static Size SilverlightSize
        {
            get
            {
                FrameworkElement rootVisual = Application.Current.RootVisual as FrameworkElement;
                if (rootVisual != null)
                {
                    return new Size(rootVisual.ActualWidth, rootVisual.ActualHeight);
                }
                return GetSize(HtmlPage.Plugin);
            }
        }
    }
}
